package stock;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class TestHangYe {

	public static void main(String[] args) {
		TestHangYe th = new TestHangYe();
		th.getCP("SZ300053");
	}

	public static String[] getCP(String code) {
		String[] all = new String[] { "", "" ,""};
		try {

			String url = "https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code="+code+"&color=b#/jyfx";
			if (driver == null) {
				driver = getdriver();
			}
			driver.get(url);

			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
			// 抓取网页内容
			// String pageContent = driver.getPageSource();

			List<WebElement> elements = driver
					.findElements(By.xpath("//div[@class='zyfxgc_table']//table[1]//tr"));
			
			 String hy = ""; boolean b1 = false;
			 String cp = ""; boolean b2 = false;
			 String qy ="";  boolean b3 = false;
			 for (int i = 0; i < elements.size(); i++) {
				 WebElement  web  = elements.get(i);
				 
				 List<WebElement>  web1 =web.findElements(By.tagName("td"));
				 if(i==0) {
					 continue;
				 }
				 if(web1.size() ==9) {
					 if(web1.get(0).getText().equals("按行业分类")) {
						 b1 = true; b2=false;b3=false;
						 hy+=web1.get(1).getText()+web1.get(3).getText()+",";
					 }
					 if(web1.get(0).getText().equals("按产品分类")) {
						 b1 = false; b2=true;b3=false;
						 cp+=web1.get(1).getText()+web1.get(3).getText()+",";
					 }
					 if(web1.get(0).getText().equals("按地区分类")) {
						 b1 = false; b2=false;b3=true;
						 qy+=web1.get(1).getText()+web1.get(3).getText()+",";
					 }
				 } 
				 if(web1.size() ==8) {
					  if(b1) {
						  hy+=web1.get(0).getText()+web1.get(2).getText()+",";	 
				     }
					 if(b2) {
						  cp+=web1.get(0).getText()+web1.get(2).getText()+",";		    	 
					 }
					 if(b3) {
						  qy+=web1.get(0).getText()+web1.get(2).getText()+",";
					 }
				 } 
			}
		 System.out.println(hy);
		 System.out.println(cp);
		 System.out.println(qy);
		 
		 all[0]=hy;
		 all[1]=cp;
		 all[2]=qy;
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("调用发生错误");
		}
		return all;
	}

	public static WebDriver driver = null;

	public static WebDriver getdriver() {

		if (driver == null) {
			File f = new File("D:\\chromedriver\\chromedriver-win64124");
			if (f.exists()) {
				System.setProperty("webdriver.chrome.driver",
						"D:\\chromedriver\\chromedriver-win64124\\chromedriver-win64\\chromedriver.exe");
			} else {
				System.setProperty("webdriver.chrome.driver", "/opt/chromedriver-linux64/chromedriver");
			}
			// 指定Chrome浏览器核心

			// 创建Chrome浏览器驱动
			// 配置Chrome选项
			ChromeOptions options = new ChromeOptions();
			options.addArguments("--headless"); // 无界面运行
			options.addArguments("--no-sandbox");
			options.addArguments("--disable-gpu");
			options.addArguments("--disable-dev-shm-usage");
			// 创建Chrome浏览器驱动
			driver = new ChromeDriver(options);
		}
		return driver;
	}

}
